package net.chery.rm;

import lombok.extern.slf4j.Slf4j;
import net.chery.entity.DataType;
import net.chery.entity.VehProduce;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author dms销售信息映射
 */
@Slf4j
public class DmsRowMapper extends VinRowMapper<VehProduce> {
    private final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DmsRowMapper(Set<String> vehModels, Consumer<VehProduce> consumer, Function<String, VehProduce> function) {
        super(vehModels,consumer,function);
    }

    @Override
    public VehProduce mapRow(ResultSet rs, int rowNum) throws SQLException {
        String vin=rs.getString("vin").trim();
        String annoucementNumber=rs.getString("annoucementnumber");
        if(!checkData(vin,annoucementNumber)){
            return null;
        }
        VehProduce produce=getNew(vin);
        String date=rs.getString("modifytime");
        if(produce.getModifyTime()!=null){
            if(date.compareTo(produce.getModifyTime())<0){
                return null;
            }
            produce.setModifyTime(date);
        } else {
            produce.setModifyTime(date);
        }
        String dmsCreateDate=rs.getString("createtime");
        if (dmsCreateDate!=null){
            produce.setDmsCreateDate(parseDate(dmsCreateDate,sdf));
        }
        if (dmsCreateDate==null){
            if(produce.getModifyTime()!=null){
                produce.setDmsCreateDate(parseDate(produce.getModifyTime(),sdf));
            }
        }
        produce.setVin(vin);
        produce.setVehModelName(annoucementNumber);
        //车牌号
        String licenseplate=rs.getString("licenseplate");
        if(StringUtils.hasLength(licenseplate)){
            produce.setLicensePlate(licenseplate);
        }
        //上牌时间
        String registrationtime=rs.getString("registrationtime");
        if(StringUtils.hasLength(registrationtime)){
            produce.setLicenseAdditionalTime(parseDate(registrationtime,sdf));
        }
        produce.setBranchCode(rs.getString("branchcode"));
        produce.setBranchName(rs.getString("branchname"));
        produce.setProductCode(rs.getString("productcode"));
        produce.setProductName(rs.getString("productname"));
        produce.setSaleTime(parseDate(rs.getString("saledate"),sdf));
        produce.setSalesDealerCode(rs.getString("salesdealercode"));
        produce.setSalesDealerName(rs.getString("salesdealername"));
        produce.setManufacturerName(rs.getString("manufacturername"));
        produce.addType(DataType.DMS);
        consumer.accept(produce);
        return produce;
    }
}
